﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Basket_session
/// </summary>
public class Basket_session
{
    private static SqlConnection sqlConnection;

    private DataTable objDataTable;
    public static SqlDataAdapter sqlAdapter;
    public static SqlCommand sqlCommand; 
    private List<OrderProduct> products;
	public Basket_session()
	{
        products = new List<OrderProduct>();
		//
		// TODO: Add constructor logic here
		//
	}
    public List<OrderProduct> ListProduct
    {
        get
        {
            return products;
        }
    }

    private int count = 0;


    public int Count
    {
        get { return count; }
    }
    public float Summary
    {
        get
        {

            float temp = 0;

            foreach (OrderProduct o in products)
            {
                temp += o.Summary;
            }
            return temp;
        }
    }


    public OrderProduct GetProductFromBasket(string _productID)
    {
        OrderProduct pro = new OrderProduct();
        pro.OrderProductID = _productID;

        int index = CheckProductExit(pro);
        if (index == -1)
            return null;
        else
            return products[index];



    }
    public void InsertProductToBasket(OrderProduct _product)
    {
        int index = CheckProductExit(_product);

        if (index == -1)//Not exit
        {
            products.Add(_product);
        }
        else//Exit then amount of this item increase
        {
            products[index].Amount += 1;
        }

        //total items in basket
        count += 1;
    }

    private int CheckProductExit(OrderProduct _product)
    {
        for (int i = 0; i < products.Count; i++)
            if (products[i].OrderProductID == _product.OrderProductID)
                return i;

        return -1;
    }

    public void DeleteProductFromBasket(OrderProduct _product)
    {
        int index = CheckProductExit(_product);
        if (index >= 0)
        //if (products.Contains(_product))
        {
            if (products[index].Amount > 1)
                products[index].Amount -= 1;
            else
                products.RemoveAt(index);
            count -= 1;
        }
    }
    public void UpdateProductFromBasket(OrderProduct _oldProduct, OrderProduct _newProduct)
    {
        int index = products.IndexOf(_oldProduct);

        if (index >= 0)
            products[index] = _newProduct;
        float i = this.Summary;
        i = products[index].Summary;
        ////reset total amount

        int temp = 0;

        foreach (OrderProduct o in products)
        {
            temp += o.Amount;
        }
        this.count = temp;
    }

    public void InsertProductIntoTable(string basketID)
    {
         foreach (OrderProduct o in products)
         {
            /*
            try
            {*/
            sqlConnection = DataProvider.CreateConnection();
            DataTable datatable = new DataTable();
            sqlAdapter = new SqlDataAdapter("select * from Basket", sqlConnection);
            sqlAdapter.Fill(datatable);

            DataRow datarow = datatable.NewRow();
            datarow[0] = int.Parse(basketID);
            datarow[1] = o.OrderProductID;
            datarow[2] = o.ProName;
            datarow[3] = o.Amount;
            datarow[4] = o.Price;


            datatable.Rows.Add(datarow);

            sqlCommand = new SqlCommand();
            sqlCommand.Connection = sqlConnection;
            sqlCommand.CommandType = CommandType.Text;
            sqlCommand.CommandText = "INSERT INTO Basket VALUES(@Basket_ID,@Pro_ID,@Pro_Name,@Pro_Amount,@Pro_Price)";
             sqlCommand.Parameters.Add("@Basket_ID", SqlDbType.Int, 10, "Basket_ID");
            sqlCommand.Parameters.Add("@Pro_ID", SqlDbType.Int, 50, "Pro_ID");
            sqlCommand.Parameters.Add("@Pro_Name", SqlDbType.NVarChar, 50, "Pro_Name");
            sqlCommand.Parameters.Add("@Pro_Amount", SqlDbType.Int, 10, "Pro_Amount");
            sqlCommand.Parameters.Add("@Pro_Price", SqlDbType.Float, 10, "Pro_Price");
            

            sqlAdapter.InsertCommand = sqlCommand;
            sqlAdapter.Update(datatable);
            sqlCommand.Dispose();
            sqlCommand = null;
            /*
        }
        catch (Exception e)
        {
            WebMsgBox.Show("ngoai le");
            return false;
        }*/
         }
    }

    public DataTable GetProductFromDatabase(string basketID)
    {
        
        return DataProvider.GetDataTable("select * from Basket where Basket.Basket_ID = "+ int.Parse(basketID) );        
           
    }
}
